#import "OmBuyKingHatVC.h"
#import "MemberApiSelectSchemeModel.h"
#import "RootWebViewController.h"
#import "OmBuyKingHatVCTableViewCell.h"
#import "OmCommonButton2.h"
#import <XYIAPKit.h>
#define CellReuseID @"OmBuyKingHatVCTableViewCell"
@interface OmBuyKingHatVC () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *listTableView;
@property (nonatomic, assign) MemberType memberType;
@property (nonatomic, strong) NSArray<MemberApiSelectSchemeModel *> *modelArray;
@property (nonatomic, strong) UIView *payButtonBottomView;
@property (nonatomic, strong) OmCommonButton2 *payButton;
@property (nonatomic, strong) UIImageView *descriptionImageView;
@end
@implementation OmBuyKingHatVC
#pragma mark - Get
- (NSArray<MemberApiSelectSchemeModel *> *)modelArray {
    MemberApiSelectSchemeModel *model = [[MemberApiSelectSchemeModel alloc] init];
    model.ID = @"11";
    model.applePayID = @"OmCrownMember";
    model.money = 99.99;
    model.day_num = @(30);
    return @[model];
}
#pragma mark - Event
- (void)eventPayButtonTap {
    [[HUDHelper sharedInstance] loading:ASLocalizedString(@"请求中..")];
    MemberApiSelectSchemeModel *model = self.modelArray[self.listTableView.indexPathForSelectedRow.row];
    [[XYStore defaultStore] addPayment:model.applePayID success:^(SKPaymentTransaction *transaction) {
        [MBProgressHUD hideHUD];
        NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"pay_api" A:@"apple_pay_member_order"];
        NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
        parameters[@"scheme_id"] = model.ID;
        [CYNET POST:url parameters:parameters responseCache:nil success:^(id responseObject) {
            NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"pay_api" A:@"apple_pay_member_verify"];
            NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
            parameters[@"order_no"] = responseObject;
            parameters[@"apple_receipt"] = [self iapReceipt];
            [CYNET POST:url parameters:parameters responseCache:nil success:^(id responseObject) {
            } failure:^(NSString *error) {
            } hasCache:NO];
        } failure:^(NSString *error) {
        } hasCache:NO];
    } failure:^(SKPaymentTransaction *transaction, NSError *error) {
        [MBProgressHUD hideHUD];
    }];
}
#pragma mark - Method
- (NSString *)iapReceipt {
    NSString *receiptString = nil;
    NSURL *rereceiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
    if ([[NSFileManager defaultManager] fileExistsAtPath:[rereceiptURL path]]) {
        NSData *receiptData = [NSData dataWithContentsOfURL:rereceiptURL];
        receiptString = [receiptData base64EncodedStringWithOptions:0];
    }
    return receiptString;
}
#pragma mark - ConstructionScript
- (instancetype)initWithType:(MemberType)type {
    self = [super init];
    if (self) {
        self.memberType = MemberTypeKingHat;
    }
    return self;
}
- (void)configSelf {
    [self changeScrollMode:ScrollModePortrait];
    [self setHiddenForTabBar:YES navigationBar:NO];
    self.title = ASLocalizedString(self.memberType == 1 ? @"升级蓝钻会员" : @"升级皇冠会员");
}
- (void)allocateComponents {
    self.listTableView = [[UITableView alloc] init].fp_delegate(self).fp_dataSource(self).fp_separatorStyle(UITableViewCellSeparatorStyleNone);
    [self.listTableView registerNib:[UINib nibWithNibName:CellReuseID bundle:nil] forCellReuseIdentifier:CellReuseID];
    self.listTableView.scrollEnabled = NO;
    [self.listTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
    [self.scrollContentView addSubview:self.listTableView];
    self.payButtonBottomView = [[UIView alloc] init].fp_backgroundColor([UIColor whiteColor]);
    [self.scrollContentView addSubview:self.payButtonBottomView];
    self.payButton = [[OmCommonButton2 alloc] initWithTitle:ASLocalizedString(@"支付")].fp_addTargetActionForControlEvents(self,@selector(eventPayButtonTap), UIControlEventTouchUpInside);
    [self.scrollContentView addSubview:self.payButton];
    self.descriptionImageView = [[UIImageView alloc] initWithImage:IMAGE_NAMED(self.memberType==MemberTypeBlueDiamond?@"OmBlueDiamondDescription":@"OmKingHatDescription")];
    [self.scrollContentView addSubview:self.descriptionImageView];
}
- (void)layoutComponents {
    [self.listTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.scrollContentView);
        make.height.equalTo(@(55*self.modelArray.count));
    }];
    [self.payButtonBottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.listTableView.mas_bottom);
        make.left.right.equalTo(self.scrollContentView);
        make.height.equalTo(@(104));
    }];
    [self.payButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.payButtonBottomView);
        make.centerY.equalTo(self.payButtonBottomView);
    }];
    [self.descriptionImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.payButtonBottomView.mas_bottom);
        make.left.right.equalTo(self.scrollContentView);
        make.height.equalTo(self.descriptionImageView.mas_width).multipliedBy(self.memberType==MemberTypeBlueDiamond?(518.0/750):(469.0/750));
        make.bottom.equalTo(self.scrollContentView);
    }];
}
#pragma mark - TableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    OmBuyKingHatVCTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellReuseID];
    cell.model = self.modelArray[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 55;
}
@end






//#import "OmBuyKingHatVC.h"
//#import "MemberApiSelectSchemeModel.h"
//#import "RootWebViewController.h"
//#import "OmBuyKingHatVCTableViewCell.h"
//#import "OmCommonButton2.h"
//#define CellReuseID @"OmBuyKingHatVCTableViewCell"
//
//@interface OmBuyKingHatVC () <UITableViewDelegate, UITableViewDataSource>
//
//@property (nonatomic, strong) UITableView *listTableView;
//@property (nonatomic, assign) MemberType type;
//@property (nonatomic, strong) NSArray<MemberApiSelectSchemeModel *> *modelArray;
//@property (nonatomic, strong) UIView *payButtonBottomView;
//@property (nonatomic, strong) OmCommonButton2 *payButton;
//@property (nonatomic, strong) UILabel *descriptionLabel;
//
//@end
//
//@implementation OmBuyKingHatVC
//
//#pragma mark - Event
//- (void)refreshSelf {
//    NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"member_api" A:@"select_scheme"];
//    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
//    parameters[@"type"] = @(self.type);
//    [CYNET POST:url parameters:parameters responseCache:nil success:^(id responseObject) {
//        self.modelArray = [MemberApiSelectSchemeModel mj_objectArrayWithKeyValuesArray:responseObject];
//        [self.listTableView reloadData];
//        [self.listTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
//        [self.listTableView mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.height.equalTo(@(55*self.modelArray.count));
//        }];
//    } failure:^(NSString *error) {
//        [MBProgressHUD showTipMessageInView:error];
//    } hasCache:NO];
//}
//
//- (void)eventPayButtonTap {
//    [[HUDHelper sharedInstance] loading:ASLocalizedString(@"请求中..")];
//    
//    MemberApiSelectSchemeModel *model = self.modelArray[self.listTableView.indexPathForSelectedRow.row];
//    NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"member_api" A:@"refill"];
//    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
//    parameters[@"scheme_id"] = model.ID;
//    [CYNET POST:url parameters:parameters responseCache:nil success:^(id responseObject) {
//        [MBProgressHUD hideHUD];
//        RootWebViewController *webView = [[RootWebViewController alloc] init];
//        webView.url = responseObject;
//        [self.navigationController pushViewController:webView animated:YES];
//    } failure:^(NSString *error) {
//        [MBProgressHUD hideHUD];
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//            [MBProgressHUD showInfoMessage:error];
//        });
//    } hasCache:NO];
//}
//
//#pragma mark - ConstructionScript
//- (instancetype)initWithType:(MemberType)type {
//    self = [super init];
//    if (self) {
//        self.type = type;
//    }
//    return self;
//}
//
//- (void)configSelf {
//    [self changeScrollMode:ScrollModePortrait];
//    [self setHiddenForTabBar:YES navigationBar:NO];
//    self.title = ASLocalizedString(self.type == 1 ? @"升级蓝钻会员" : @"升级皇冠会员");
//}
//
//- (void)allocateComponents {
//    self.listTableView = [[UITableView alloc] init].fp_delegate(self).fp_dataSource(self).fp_separatorStyle(UITableViewCellSeparatorStyleNone);
//    [self.listTableView registerNib:[UINib nibWithNibName:CellReuseID bundle:nil] forCellReuseIdentifier:CellReuseID];
//    self.listTableView.scrollEnabled = NO;
//    [self.scrollContentView addSubview:self.listTableView];
//    
//    self.payButtonBottomView = [[UIView alloc] init].fp_backgroundColor([UIColor whiteColor]);
//    [self.scrollContentView addSubview:self.payButtonBottomView];
//    
//    self.payButton = [[OmCommonButton2 alloc] initWithTitle:ASLocalizedString(@"支付")].fp_addTargetActionForControlEvents(self,@selector(eventPayButtonTap), UIControlEventTouchUpInside);
//    [self.scrollContentView addSubview:self.payButton];
//    
//    self.descriptionLabel = [[UILabel alloc] init].fp_font([UIFont systemFontOfSize:14]).fp_numberOfLines(0);
//    NSMutableAttributedString *diamondString = [[NSMutableAttributedString alloc] init]
//    .fp_appendAttributedString(ASLocalizedString(@"蓝钻会员\n"), [UIFont systemFontOfSize:18], HexadecimalColor(@"E589B2"))
//    .fp_appendAttributedString(ASLocalizedString(@"1、自由聊天\n"), nil, nil)
//    .fp_appendAttributedString(ASLocalizedString(@"2、自由查看新鲜事\n"), nil, nil);
//    diamondString.lineSpacing = 5;
//    
//    NSMutableAttributedString *crownString = [[NSMutableAttributedString alloc] init]
//    .fp_appendAttributedString(ASLocalizedString(@"皇冠会员\n"), [UIFont systemFontOfSize:18], HexadecimalColor(@"E589B2"))
//    .fp_appendAttributedString(ASLocalizedString(@"1、获得蓝钻会员的所有权限\n"), nil, nil)
//    .fp_appendAttributedString(ASLocalizedString(@"2、发送语音\n"), nil, nil)
//    .fp_appendAttributedString(ASLocalizedString(@"3、发送照片\n"), nil, nil)
//    .fp_appendAttributedString(ASLocalizedString(@"4、拍照\n"), nil, nil)
//    .fp_appendAttributedString(ASLocalizedString(@"5、获得翻译功能\n"), nil, nil)
//    .fp_appendAttributedString(ASLocalizedString(@"6、自由发布、评论新鲜事\n\n"), nil, nil)
//    .fp_appendAttributedString(ASLocalizedString(@"6个月以上皇冠会员可以获得视频聊天功能"), nil, nil);
//    crownString.lineSpacing = 5;
//    
//    self.descriptionLabel.fp_attributedText(self.type == MemberTypeKingHat ? crownString : diamondString);
//    
//    [self.scrollContentView addSubview:self.descriptionLabel];
//}
//
//- (void)layoutComponents {
//    [self.listTableView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.left.right.equalTo(self.scrollContentView);
//    }];
//    
//    [self.payButtonBottomView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.listTableView.mas_bottom);
//        make.left.right.equalTo(self.scrollContentView);
//        make.height.equalTo(@(104));
//    }];
//    
//    [self.payButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.equalTo(self.payButtonBottomView);
//        make.centerY.equalTo(self.payButtonBottomView);
//    }];
//    
//    [self.descriptionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.payButtonBottomView.mas_bottom).offset(20);
//        make.left.equalTo(self.scrollContentView).offset(20);
//        make.right.equalTo(self.scrollContentView).offset(-20);
//        make.bottom.equalTo(self.scrollContentView).offset(-20);
//    }];
//}
//
//#pragma mark - TableView
//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    return self.modelArray.count;
//}
//
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    OmBuyKingHatVCTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellReuseID];
//    cell.model = self.modelArray[indexPath.row];
//    return cell;
//}
//
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    return 55;
//}
//
//
//
//@end
